package com.yicoollxing.util;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateTimeUtil {
	// joda-time
	private static String STANDARD_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public static Date strToDate(String dateStr, String strPattern) {

		DateTimeFormatter format = DateTimeFormat.forPattern(strPattern);
		DateTime dateTime = format.parseDateTime(dateStr);
		return dateTime.toDate();
	}

	public static String dateToStr(Date date, String strPattern) {
		if (date == null) {
			return StringUtils.EMPTY;
		}
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(strPattern);
	}

	public static Date strToDate(String dateStr) {

		DateTimeFormatter format = DateTimeFormat.forPattern(STANDARD_PATTERN);
		DateTime dateTime = format.parseDateTime(dateStr);
		return dateTime.toDate();
	}

	public static String dateToStr(Date date) {
		if (date == null) {
			return StringUtils.EMPTY;
		}
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(STANDARD_PATTERN);
	}
}
